#ifndef WindowPort_First
#define WindowPort_First
#ifdef __GNUG__
#pragma interface
#endif

#include "WindowSystem.h"

class Window;
class OldRegion;
class RGB;
class WindowColorMap;
class DevBitmap;

enum WinState {
	eWsHidden,    // window allocated but not in hierarchy
	eWsShown,     // window is inserted in window hierarchy
	eWsIconic,
	eWsClosed     // window will be removed
};

//---- WindowPort --------------------------------------------------------------

class WindowPort: public Port {
protected:
public:
	Window *win;
	bool overlay, exitloop;
	GrCursor cursor;
	WinState state;
	OldRegion *inval;
	Point lastpos;
	Rectangle portrect;
	WindowColorMap *wcmap;

	//---- overridden ----------------------------------------------------
	bool DevImageCacheBegin(ImageCache*, const Rectangle&);
	void DevImageCacheEnd(ImageCache*);
	//--------------------------------------------------------------------

	void OpenNotify(const Rectangle &r);
	void CloseNotify();

public:
	MetaDef(WindowPort);
	WindowPort(Window *bw, WindowType wt, GrCursor curs);
	~WindowPort();

	//---- device dependent stuff ----------------------------------------
	virtual int DevGetId();
	virtual void DevUpdate();

	virtual void DevFullscreen(bool);
	virtual void DevScrollRect(const Rectangle&, const Point&);
	virtual void DevHide();
	virtual void DevIconize();
	virtual void DevShow(WindowPort *father, const Rectangle&);
	virtual void DevSetMousePos(const Point&, bool);
	virtual void DevBell(long d);
	virtual void DevTop(bool);
	virtual void DevSetCursor(GrCursor);
	virtual void DevSetExtent(const Point&);
	virtual void DevSetOrigin(const Point&);
	virtual Rectangle DevGetRect();
	virtual void DevSetTitle(const char*);

	void DevSetColor(RGBColor *cp);
	virtual void DevSetColor2(u_long id);
	//---- end of device dependent stuff ---------------------------------
		
	//---- window management ---------------------------------------------
	void Top()      // bring window to top of window stack
		{ DevTop(TRUE); }
	void Bottom()   // bring window to bottom of window stack
		{ DevTop(FALSE); }
	void Hide();    // remove a window temporary from the screen
	void Show(WindowPort *father, const Rectangle&, bool block);
					// reinsert a previously hidden window
	void Iconize();
	void UpdateRect(const Rectangle &r);

	void SetTitle(const char *title)
		{ DevSetTitle(title); }

	//---- size management -----------------------------------------------
	Rectangle GetRect()
		{ return DevGetRect(); }
	void SetOrigin(const Point &o)
		{ DevSetOrigin(o); }
	void SetExtent(const Point &e)
		{ DevSetExtent(e); }

	//---- scrolling -------------------------------------------------------
	void Scroll(const Point&);
	void InvalidateRect(const Rectangle &r, bool update= FALSE);

	//---- input handling ------------------------------------------------
	void InputNotify(Token *t);
	void GetEvent(Token *t);            // read next event

	void Fullscreen(bool m);            // fullscreen mode

	void SetMousePos(const Point &p)    // set the mouse position in window
		{ DevSetMousePos(p, FALSE); }   // relative coordinates

	void MoveMousePos(const Point &delta)  // move the mouse position
		{ DevSetMousePos(delta, TRUE); }

	void Bell(long d)               // ring the bell
		{ DevBell(d); }

	void GrabLoop();
	void Poll(int timeout= 300);
	void ExitLoop()
		{ exitloop= TRUE; }

	WindowColorMap *ColorMap()
		{ return wcmap; }
	void FlushColorMap();

	//---- cursor stuff --------------------------------------------------
	GrCursor GetCursor();
	GrCursor SetWaitCursor(unsigned int d= 400, GrCursor c= eCrsHourglass);
	GrCursor SetCursor(GrCursor);

	bool DragDropNotify(int code, const Point &p, Rectangle *rp);
};

struct batchInfo {
	DevBitmap *b;
	Rectangle r;
	Point p;
};

#endif
